//
//  NDCitem.m
//  iMed

//	NDCitem is anything with an NDC number that may be prescribed.

//
//  Created by Barry Press on 8/14/09.
//  Copyright 2009 Barry Press. All rights reserved.
//

#import "NDCitem.h"


@implementation NDCitem


#pragma Copying method
// =============================================================================
// ---------------------------------------------- copyWithZone -----------------
- (id)copyWithZone:(NSZone *)zone
{
	NDCitem* i = [[NDCitem allocWithZone:zone] init];
	[i setItem:[self item]];
	return i;
}

#pragma mark Archiving methods
// =============================================================================
// ---------------------------------------------- dealloc ----------------------
- (void)dealloc
{
	NSAssert(item != NULL, @"NULL array pointer in NDCitem:dealloc");
	[item release];
	item = NULL;
	[super dealloc];
}

// ---------------------------------------------- description ------------------
- (NSString*)description
{
	NSAssert(item != NULL, @"No dictionary in NDCitem");
	
	NSString *s = [NSString stringWithFormat:@"{NDC[%p]:%@, Name:%@, Quantity:%@, Unit:%@}", 
				  self, [self ndc], [self name], [self quantity], [self unit]];
	return s;
}

// ---------------------------------------------- encodeWithCoder --------------
- (void)encodeWithCoder:(NSCoder *)coder
{
	NSAssert(item != NULL, @"NULL array pointer in NDCitem:encodeWithCoder");
	[coder encodeObject:item forKey:@"Mitem"];
}

// ---------------------------------------------- init -------------------------
- (id)init
{
	[super init];

	item = [[NSMutableDictionary dictionaryWithCapacity:4] retain];
	NSAssert(item != NULL, @"NULL array pointer in NDCitem:init");

	[self setNdc:nil];
	[self setName:nil];
	[self setQuantity:nil];
	[self setUnit:nil];
	return self;
}

// ---------------------------------------------- initWithCoder ----------------
- (id)initWithCoder:(NSCoder *)coder
{
	[super init];
	item = [[coder decodeObjectForKey:@"Mitem"] retain];
	return self;
}

// ---------------------------------------------- initWithNDC:Name:Quantity:Unit: --
- (id)initWithNDC:(NSString*)sNDC Name:(NSString*)sName Quantity:(NSString*)sQuantity Unit:(NSString*)sUnit
{
	[self init];
	[item setObject:sNDC forKey:@"NDC"];
	[item setObject:sName forKey:@"Name"];
	[item setObject:sQuantity forKey:@"Quantity"];
	[item setObject:sUnit forKey:@"Unit"];
	return self;
}

// ---------------------------------------------- setObject:forKey -------------
- (void)setObject:(id)anObject forKey:(id)aKey
{
	NSAssert(item != NULL, @"NULL array pointer in NDCitem:setObject:forKey");
	[item setObject:anObject forKey:aKey];
}

// ---------------------------------------------- keyEnumerator ----------------
- (NSEnumerator*)keyEnumerator
{
	NSAssert(item != NULL, @"NULL array pointer in NDCitem:keyEnumerator");
	return [item keyEnumerator];
}

// ---------------------------------------------- objectAtKey ------------------
- (id)objectAtKey:(id)aKey
{
	NSAssert(item != NULL, @"NULL array pointer in NDCitem:objectAtKey");
	return [item objectForKey:aKey];
}


#pragma mark Accessor methods

// ---------------------------------------------- accessors --------------------
- (BOOL)validateObject
{
	NSAssert(self != NULL, @"NULL self object in NDCitem"); 
	return self != nil && item != NULL && item != nil && [item isKindOfClass:[NSMutableDictionary class]];
}

// ---------------------------------------------- validatedString --------------
- (NSString*)validatedString:(NSString*)key
{
	NSString* value = @"";
	
	if ([self validateObject]) {
		value = [item objectForKey:key];
		if (value == nil) {
			value == @"";
		}
	}
	//NSLog(@"Key \"%@\" value for NDC %p is %@", key, self, [item objectForKey:key]);
	return value ;
}

// ---------------------------------------------- ndc --------------------------
- (NSString*)ndc
{
	return [self validatedString:@"NDC"];
}

- (void)setNdc:(NSString*)aString
{
	NSAssert(item != NULL, @"NULL array pointer in NDCitem:setNdc");
	[item setValue:aString forKey:@"NDC"];
}

// ---------------------------------------------- name -------------------------
- (NSString*)name
{
	return [self validatedString:@"Name"];
}

- (void)setName:(NSString*)aString
{
	NSAssert(item != NULL, @"NULL array pointer in NDCitem:setName");
	[item setValue:aString forKey:@"Name"];
}

// ---------------------------------------------- quantity ---------------------
- (NSString*)quantity
{
	return [self validatedString:@"Quantity"];
}

- (void)setQuantity:(NSString*)aString
{
	NSAssert(item != NULL, @"NULL array pointer in NDCitem:setQuantity");
	[item setValue:aString forKey:@"Quantity"];
}

// ---------------------------------------------- unit -------------------------
- (NSString*)unit
{
	return [self validatedString:@"Unit"];
}

- (void)setUnit:(NSString*)aString
{
	NSAssert(item != NULL, @"NULL array pointer in NDCitem:setUnit");
	[item setValue:aString forKey:@"Unit"];
}

@synthesize item;

@end
